﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Threading;
using com.fukefu.spider;
using com.fukefu.sql;
using com.fukefu.tools;

namespace com.fukefu.url
{
    class UrlSholu
    {
        public event MyDelegate2 GetControl;
        public event MyDelegate3 SetControl;
        Queue<WebUrl> web_queue = new Queue<WebUrl>();
        Spider baidu = new BaiduSpider();
        SqlServerOperate sql = null;
        //检测网址是否收录
        public void CheckWebPagesShouLu(Object canshu)
        {
            sql = new SqlServerOperate();
            SqlDataReader re;
            //获取数据库名称
            List<string> list = new List<string>();
            if (LoginUser.Uid == null)
            {
                re = sql.Query("select name from master..sysdatabases where name like '%智能群发%'");
                while (re.Read())
                {
                    list.Add(re["name"].ToString());
                }
                re.Close();
                sql.Close();
            }
            else
            {
                list.Add(Config.Sql_database);
            }
            do
            {
                foreach (string database in list)
                {
                    Config.Sql_database = database;
                    sql = new SqlServerOperate();
                    web_queue = new Queue<WebUrl>();
                    //加载所有代码
                    re = sql.Query("select * from [发布总表] where " + canshu + " and shoulu=0 order by id desc");
                    while (re.Read())
                    {
                        web_queue.Enqueue(new WebUrl(re["id"].ToString(), re["url"].ToString()));
                    }
                    re.Close();
                    //开始检测
                    CheckOneWebPage();
                    //检测结束
                    Display(database + " 收录检测完成");
                    sql.Close();
                    Thread.Sleep(30 * 1000);
                }
                Thread.Sleep(90 * 1000);
            } while (Convert.ToBoolean(GetControl("checkBox1", ControlValue.Checked)));
            if (SetControl != null)
            {
                SetControl("button1", ControlValue.Enable, true);
            }
        }
        //检测一个网页
        private void CheckOneWebPage()
        {
            WebUrl site;
            while (true)
            {
                lock (web_queue)
                {
                    if (web_queue.Count == 0)
                    {
                        break;
                    }
                    site = web_queue.Dequeue();
                }
                if (baidu.CheckShoulu(site.Url))
                {
                    sql.Update("update [已发布] set shoulu=1,[dt_shoulu]=getdate() where id=" + site.Uid);
                }
                else
                {
                    Display("没有收录 " + site.Url);
                }
            }
        }
        //窗口显示
        private void Display(string message)
        {
            if (SetControl != null)
            {
                SetControl("listbox1", ControlValue.ListBox, message);
            }
        }
    }
}
